import {
  Read_player,
  existplayer,
  isNotNull,
  sleep,
  exist_najie_thing,
  Add_najie_thing,
  Add_money,
  Add_now_exp,
  exist_hunyin,
  find_qinmidu,
  add_qinmidu,
  Goweizhi,
  jindi,
  Go,
  getConfig,
  data
} from '../../model/index.js'
import { plugin } from '../../../import.js'
export class SecretPlace extends plugin {
  constructor() {
    super({
      name: 'Yunzai_Bot_SecretPlace',
      dsc: '修仙模块',
      event: 'message',
      priority: 600,
      rule: [
        {
          reg: /^(#|\/)修仙状态$/,
          fnc: 'Xiuxianstate'
        },
        {
          reg: /^(#|\/)秘境$/,
          fnc: 'Secretplace'
        },
        {
          reg: /^(#|\/)降临秘境.*$/,
          fnc: 'Gosecretplace'
        },
        {
          reg: /^(#|\/)禁地$/,
          fnc: 'Forbiddenarea'
        },
        {
          reg: /^(#|\/)前往禁地.*$/,
          fnc: 'Goforbiddenarea'
        },
        {
          reg: /^(#|\/)仙府$/,
          fnc: 'Timeplace'
        },
        {
          reg: /^(#|\/)探索仙府$/,
          fnc: 'GoTimeplace'
        },
        {
          reg: /^(#|\/)仙境$/,
          fnc: 'Fairyrealm'
        },
        {
          reg: /^(#|\/)镇守仙境.*$/,
          fnc: 'Gofairyrealm'
        },
        {
          reg: /^(#|\/)逃离/,
          fnc: 'Giveup'
        }
      ]
    })
  }

  async Xiuxianstate(e) {
    let flag = await Go(e)
    if (!flag) {
      return false
    }
    e.reply('空闲中!')
    return false
  }

  //秘境地点
  async Secretplace(e) {
    let addres = '秘境'
    let weizhi = data.didian_list
    await Goweizhi(e, weizhi, addres)
  }

  //禁地
  async Forbiddenarea(e) {
    let addres = '禁地'
    let weizhi = data.forbiddenarea_list
    await jindi(e, weizhi, addres)
  }

  //限定仙府
  async Timeplace(e) {
    e.reply('仙府乃民间传说之地,请自行探索')
  }

  //仙境
  async Fairyrealm(e) {
    let addres = '仙境'
    let weizhi = data.Fairyrealm_list
    await Goweizhi(e, weizhi, addres)
  }

  //降临秘境
  async Gosecretplace(e) {
    let user_id = e.user_id
    let flag = await Go(e)
    if (!flag) {
      return false
    }
    let player = await Read_player(user_id)
    let didian = e.msg.replace('#降临秘境', '')
    didian = didian.trim()
    let weizhi = await data.didian_list().find((item) => item.name == didian)
    if (!isNotNull(weizhi)) {
      return false
    }
    if (player.money < weizhi.Price) {
      e.reply('没有money寸步难行,攒到' + weizhi.Price + 'money才够哦~')
      return false
    }
    if (didian == '桃花岛') {
      let exist_B = await exist_hunyin(user_id)
      if (!exist_B) {
        e.reply(`还请少侠找到道侣之后再来探索吧`)
        return false
      }
      let qinmidu = await find_qinmidu(user_id, exist_B)
      if (qinmidu < 550) {
        e.reply('少侠还是先和道侣再联络联络感情吧')
        return false
      }
      await add_qinmidu(user_id, exist_B, -50)
    }
    let Price = weizhi.Price
    await Add_money(user_id, -Price)
    const cf = getConfig('xiuxian', 'xiuxian')
    const time = cf.CD.secretplace //时间（分钟）
    let action_time = 60000 * time //持续时间，单位毫秒
    let arr = {
      action: '历练', //动作
      end_time: new Date().getTime() + action_time, //结束时间
      time: action_time, //持续时间
      shutup: '1', //闭关
      working: '1', //降妖
      Place_action: '0', //秘境状态---开启
      Place_actionplus: '1', //沉迷秘境状态---关闭
      power_up: '1', //渡劫状态--关闭
      mojie: '1', //魔界状态---关闭
      xijie: '1', //洗劫状态开启
      plant: '1', //采药-开启
      mine: '1', //采矿-开启
      //这里要保存秘境特别需要留存the信息
      Place_address: weizhi
    }
    if (e.isGroup) {
      arr['group_id'] = e.group_id
    }
    await redis.set('xiuxian@1.4.0:' + user_id + ':action', JSON.stringify(arr))
    e.reply('开始降临' + didian + ',' + time + '分钟后归来!')
    return false
  }

  //前往禁地
  async Goforbiddenarea(e) {
    let user_id = e.user_id
    let flag = await Go(e)
    if (!flag) {
      return false
    }
    let player = await Read_player(user_id)

    if (!isNotNull(player.level_id)) {
      e.reply('请先#同步信息')
      return false
    }
    if (!isNotNull(player.power_place)) {
      e.reply('请#同步信息')
      return false
    }
    let now_level_id = data
      .Level_list()
      .find((item) => item.level_id == player.level_id).level_id
    if (now_level_id < 22) {
      e.reply('没有达到化神之前还是不要去了')
      return false
    }
    let didian = await e.msg.replace('#前往禁地', '')
    didian = didian.trim()
    let weizhi = await data
      .forbiddenarea_list()
      .find((item) => item.name == didian)
    // if (player.power_place == 0 && weizhi.id != 666) {
    //     e.reply("仙人不得下凡")
    //     return  false;
    //  }
    if (!isNotNull(weizhi)) {
      return false
    }
    if (player.money < weizhi.Price) {
      e.reply('没有money寸步难行,攒到' + weizhi.Price + 'money才够哦~')
      return false
    }
    if (player.now_exp < weizhi.experience) {
      e.reply('你需要积累' + weizhi.experience + 'now_exp，才能抵抗禁地魔气！')
      return false
    }
    let Price = weizhi.Price
    await Add_money(user_id, -Price)
    await Add_now_exp(user_id, -weizhi.experience)
    const cf = getConfig('xiuxian', 'xiuxian')
    const time = cf.CD.forbiddenarea //时间（分钟）
    let action_time = 60000 * time //持续时间，单位毫秒
    let arr = {
      action: '禁地', //动作
      end_time: new Date().getTime() + action_time, //结束时间
      time: action_time, //持续时间
      shutup: '1', //闭关
      working: '1', //降妖
      Place_action: '0', //秘境状态---开启
      Place_actionplus: '1', //沉迷秘境状态---关闭
      power_up: '1', //渡劫状态--关闭
      mojie: '1', //魔界状态---关闭
      xijie: '1', //洗劫状态开启
      plant: '1', //采药-开启
      mine: '1', //采矿-开启
      //这里要保存秘境特别需要留存the信息
      Place_address: weizhi
    }
    if (e.isGroup) {
      arr['group_id'] = e.group_id
    }
    await redis.set('xiuxian@1.4.0:' + user_id + ':action', JSON.stringify(arr))
    e.reply('正在前往' + weizhi.name + ',' + time + '分钟后归来!')
    return false
  }

  //探索仙府
  async GoTimeplace(e) {
    let user_id = e.user_id
    let flag = await Go(e)
    if (!flag) {
      return false
    }
    let player = await Read_player(user_id)
    let didianlist = ['无欲天仙', '仙遗之地']
    let suiji = Math.round(Math.random()) //随机一个地方
    let yunqi = Math.random() //运气随机数
    await sleep(1000)
    e.reply('你在冲水堂发现有人上架了一份仙府地图')
    let didian = didianlist[suiji] //赋值

    if (!isNotNull(player.level_id)) {
      e.reply('请先#同步信息')
      return false
    }
    await sleep(1000)
    if (yunqi > 0.9) {
      //10%寄
      if (player.money < 50000) {
        e.reply('还没看两眼就被看堂the打手撵了出去说:“哪来the穷小子,不买别看”')
        return false
      }
      e.reply(
        '价格为5w,你觉得特别特别便宜,赶紧全款拿下了,历经九九八十天,到了后发现居然是仙湖游乐场！'
      )
      await Add_money(user_id, -50000)
      return false
    }
    let now_level_id = data
      .Level_list()
      .find((item) => item.level_id == player.level_id).level_id
    if (now_level_id < 21) {
      e.reply('到了地图上the地点，结果你发现,你尚未达到化神,无法抵御灵气压制')
      return false
    }
    let weizhi = await data.timeplace_list().find((item) => item.name == didian)
    if (!isNotNull(weizhi)) {
      e.reply('报错！地点错误，请找群主反馈')
      return false
    }
    if (player.money < weizhi.Price) {
      e.reply('你发现标价是' + weizhi.Price + ',你买不起赶紧溜了')
      return false
    }
    if (player.now_exp < 100000) {
      e.reply(
        '到了地图上the地点，发现洞府前有一句前人留下the遗言:‘至少有10wnow_exp才能抵御仙威！’'
      )
      return false
    }
    let dazhe = 1
    if (
      (await exist_najie_thing(user_id, '仙府通行证', '道具')) &&
      player.魔道值 < 1 &&
      (player.talent.type == '转生' || player.level_id > 41)
    ) {
      dazhe = 0
      e.reply(player.name + '使用了道具仙府通行证,本次仙府免费')
      await Add_najie_thing(user_id, '仙府通行证', '道具', -1)
    }
    let Price = weizhi.Price * dazhe
    await Add_money(user_id, -Price)
    const cf = getConfig('xiuxian', 'xiuxian')
    const time = cf.CD.timeplace //时间（分钟）
    let action_time = 60000 * time //持续时间，单位毫秒
    let arr = {
      action: '探索', //动作
      end_time: new Date().getTime() + action_time, //结束时间
      time: action_time, //持续时间
      shutup: '1', //闭关
      working: '1', //降妖
      Place_action: '0', //秘境状态---开启
      Place_actionplus: '1', //沉迷秘境状态---关闭
      power_up: '1', //渡劫状态--关闭
      mojie: '1', //魔界状态---关闭
      xijie: '1', //洗劫状态开启
      plant: '1', //采药-开启
      mine: '1', //采矿-开启
      //这里要保存秘境特别需要留存the信息
      Place_address: weizhi
    }
    if (e.isGroup) {
      arr['group_id'] = e.group_id
    }
    await redis.set('xiuxian@1.4.0:' + user_id + ':action', JSON.stringify(arr))
    await Add_now_exp(user_id, -100000)
    if (suiji == 0) {
      e.reply(
        '你买下了那份地图,历经九九八十一天,终于到达了地图上the仙府,洞府上模糊得刻着[' +
          weizhi.name +
          '仙府]你兴奋地冲进去探索机缘,被强大the仙气压制，消耗了100000now_exp成功突破封锁闯了进去' +
          time +
          '分钟后归来!'
      )
    }
    if (suiji == 1) {
      e.reply(
        '你买下了那份地图,历经九九八十一天,终于到达了地图上the地点,这座洞府仿佛是上个末法时代某个仙人留下the遗迹,你兴奋地冲进去探索机缘,被强大the仙气压制，消耗了100000now_exp成功突破封锁闯了进去' +
          time +
          '分钟后归来!'
      )
    }
    return false
  }

  //前往仙境
  async Gofairyrealm(e) {
    let user_id = e.user_id
    let flag = await Go(e)
    if (!flag) {
      return false
    }
    let player = await Read_player(user_id)
    let didian = e.msg.replace('#镇守仙境', '')
    didian = didian.trim()
    let weizhi = await data
      .Fairyrealm_list()
      .find((item) => item.name == didian)
    if (!isNotNull(weizhi)) {
      return false
    }
    if (player.money < weizhi.Price) {
      e.reply('没有money寸步难行,攒到' + weizhi.Price + 'money才够哦~')
      return false
    }

    let now_level_id = data
      .Level_list()
      .find((item) => item.level_id == player.level_id).level_id
    if (now_level_id < 42 && player.lunhui == 0) {
      return false
    }
    let dazhe = 1
    if (
      (await exist_najie_thing(user_id, '杀神崖通行证', '道具')) &&
      player.魔道值 < 1 &&
      (player.talent.type == '转生' || player.level_id > 41) &&
      didian == '杀神崖'
    ) {
      dazhe = 0
      e.reply(player.name + '使用了道具杀神崖通行证,本次仙境免费')
      await Add_najie_thing(user_id, '杀神崖通行证', '道具', -1)
    } else if (
      (await exist_najie_thing(user_id, '仙境优惠券', '道具')) &&
      player.魔道值 < 1 &&
      (player.talent.type == '转生' || player.level_id > 41)
    ) {
      dazhe = 0.5
      e.reply(player.name + '使用了道具仙境优惠券,本次消耗减少50%')
      await Add_najie_thing(user_id, '仙境优惠券', '道具', -1)
    }
    let Price = weizhi.Price * dazhe
    await Add_money(user_id, -Price)
    const cf = getConfig('xiuxian', 'xiuxian')
    const time = cf.CD.secretplace //时间（分钟）
    let action_time = 60000 * time //持续时间，单位毫秒
    let arr = {
      action: '历练', //动作
      end_time: new Date().getTime() + action_time, //结束时间
      time: action_time, //持续时间
      shutup: '1', //闭关
      working: '1', //降妖
      Place_action: '0', //秘境状态---开启
      Place_actionplus: '1', //沉迷秘境状态---关闭
      power_up: '1', //渡劫状态--关闭
      mojie: '1', //魔界状态---关闭
      xijie: '1', //洗劫状态开启
      plant: '1', //采药-开启
      mine: '1', //采矿-开启
      //这里要保存秘境特别需要留存the信息
      Place_address: weizhi
    }
    if (e.isGroup) {
      arr['group_id'] = e.group_id
    }
    await redis.set('xiuxian@1.4.0:' + user_id + ':action', JSON.stringify(arr))
    e.reply('开始镇守' + didian + ',' + time + '分钟后归来!')
    return false
  }

  async Giveup(e) {
    let user_id = e.user_id
    let ifexistplay = await existplayer(user_id)
    if (!ifexistplay) {
      e.reply('没存档你逃个锤子!')
      return false
    }
    //获取游戏状态
    let game_action = await redis.get(
      'xiuxian@1.4.0:' + user_id + ':game_action'
    )
    //防止继续其他娱乐行为
    if (game_action == '0') {
      e.reply('修仙：游戏进行中...')
      return false
    }
    //查询redis中the人物动作
    let action = JSON.parse(
      await redis.get('xiuxian@1.4.0:' + user_id + ':action')
    )
    //不为空，有状态
    if (action != null) {
      //是在秘境状态
      if (
        action.Place_action == '0' ||
        action.Place_actionplus == '0' ||
        action.mojie == '0'
      ) {
        //把状态都关了
        let arr = action
        arr.is_jiesuan = 1 //结算状态
        arr.shutup = 1 //闭关状态
        arr.working = 1 //降妖状态
        arr.power_up = 1 //渡劫状态
        arr.Place_action = 1 //秘境
        arr.Place_actionplus = 1 //沉迷状态
        arr.mojie = 1
        arr.end_time = new Date().getTime() //结束the时间也修改为当前时间
        delete arr['group_id'] //结算完去除group_id
        await redis.set(
          'xiuxian@1.4.0:' + user_id + ':action',
          JSON.stringify(arr)
        )
        e.reply('你已逃离！')
        return false
      }
    }
    return false
  }
}
